package com.zkj.controller.user;


import com.zkj.constant.StatusConstant;
import com.zkj.entity.Dish;
import com.zkj.result.Result;
import com.zkj.service.IDishService;
import com.zkj.vo.DishVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController("userDishController")
@RequestMapping("/user/dish")
@Component("userDishController")
@Slf4j
@Api(tags = "C端-菜品浏览接口")
public class DishController {
    @Autowired
    private IDishService dishService;

    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 根据分类id查询菜品
     *
     * @param categoryId
     * @return
     */
    @GetMapping("/list")
    @ApiOperation("根据分类id查询菜品")
    public Result<List<DishVO>> list(Long categoryId) {

        String key = "dish_" + categoryId;
        // 判断有缓存，直接返回
        List<DishVO> list = (List<DishVO>) redisTemplate.opsForValue().get(key);
        if (list != null) {
            return Result.success(list);
        }

        // 判断没有缓存，查询数据库，并存入缓存
        List<DishVO> listResult = dishService.listWithFlavor(categoryId);
        redisTemplate.opsForValue().set(key, listResult);

        return Result.success(listResult);
    }
}
